<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
$(document).ready(function(){
	$("#save").click(function() {
		
		var isNew = "${isNew}";
		var isNext = true;
		
		if($("#categoryCd").val() == '') {
			alert("공통코드를 입력해 주세요.");
			return false;
		}
		
		if($("#categoryNm").val() == '') {
			alert("공통코드명을 입력해 주세요.");
			return false;
		}
		
		if(isNew == 'true') {
			var params = {
				catCd: $("#categoryCd").val()
			};
			
			$.ajax({
				url: "/manage/category/check",
				type: "GET",
				async: false,
				data: params,
				success: function(result) {
					var statusCode = JSON.parse(result).statusCode;
					if(statusCode > 0) {
						alert("이미 존재하는 공통코드 입니다.");
						isNext = false;
					}
				}
			});
		}
		
		if(isNext) {
			if(confirm("저장하시겠습니까?")) {
				var model = $('#categoryModel').serialize();
				enco.log(model);
				$.post("/manage/category/write", model, function(result) {
					var statusCode = JSON.parse(result).statusCode;
					
					if(statusCode > 0) {
						alert("정상적으로 저장되었습니다.");
						opener.location.replace('/manage/category');
						window.close();
					} else {
						alert("오류가 발생하였습니다.");
					}
				});
			}
		}
		
		return false;
	});
	
	$("#close").click(function(){
		parent.close();
	});
});
</script>
</head>

<body>
<div class="content_top">
	<p class="path">
		<strong>공통코드</strong>
	</p>
</div>
<h4>시스템의 모든 공통코드를, 등록, 수정할 수 있습니다.</h4>

<form:form modelAttribute="categoryModel" action="/manage/category/write" method="post">
<div class="tbl_type3">
	<table cellspacing="0" border="1">
	<col width="120">
	<col>
	<tr>
		<th>공통코드</th>
		<td>
			<input id="categoryCd" name="categoryCd" type="text" class="input_txt" style="width: 100%" value="${categoryModel.categoryCd}">
		</td>
	</tr>
	<tr>
		<th>공통코드명</th>
		<td>
			<input id="categoryNm" name="categoryNm" type="text" class="input_txt" style="width: 100%" value="${categoryModel.categoryNm}">
		</td>
	</tr>
	<tr>
		<th>비고</th>
		<td>
			<textarea id="categoryDesc" name="categoryDesc" class="input_txt" style="width: 100%; height: 150px;" rows="10" cols="10">${categoryModel.categoryDesc}</textarea>
		</td>
	</tr>
	</table>
</div>
<br>

<div class="div_right">
	<a href="javascript:;" class="btn_big" id="save"><strong><c:if test="${isNew}">저장</c:if><c:if test="${not isNew}">수정</c:if></strong></a>
	<a href="javascript:;" class="btn_big" id="close"><strong>닫기</strong></a>
</div>
</form:form>
</body>
</html>